//
//  RHMQEndPointNodesController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/8/15.
//

#import "RHMQEndPointNodesController.h"
#import "RHMQAddEndPointController.h"
#import "RHMQEndPointModel.h"


@interface RHMQEndPointNodesController ()<UITableViewDelegate ,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,strong) RHMQEndPointModel *superModel;


@end

@implementation RHMQEndPointNodesController

- (instancetype)initWithModel:(RHMQEndPointModel *)model{
    
    self = [super init];
    if(self){
        self.superModel = model;
        if(!self.superModel.nodes){
            self.superModel.nodes = [NSMutableArray array];
        }
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"Nodes"];
    [self.view setBackgroundColor:CFTabViewBgColor];
    [self setupUI];
}


- (void)setupUI{
    
    QMUIButton *addBtn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setFrame:CGRectMake(0, 0,40, 64)];
    [addBtn setBackgroundColor:UIColor.clearColor];
    [addBtn setTitleColor:CFMainColor forState:UIControlStateNormal];
    [addBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [addBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [addBtn setTitle:@"新增" forState:UIControlStateNormal];
    UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:addBtn];
    self.navigationItem.rightBarButtonItem = rightBarBtn;
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:CFTabViewBgColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    kWeakSelf;
    [[addBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf addEndPoint];
    }];
}

#pragma mark - Action
- (void)addEndPoint{
    RHMQAddEndPointController *vc = [[RHMQAddEndPointController alloc] initWithModel:self.superModel];
    [self.navigationController pushViewController:vc animated:YES];
    kWeakSelf;
    vc.callBackBlock = ^(RHMQEndPointModel * _Nonnull model) {
          [weakSelf.superModel.nodes addObject:model];
          [weakSelf.tableView reloadData];
    };
}

#pragma mark - UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.superModel.nodes.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 200.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200)];
    return footView;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
    [cell.textLabel setTextColor:kCOLOR_HEX(@"666666")];

    
    RHMQEndPointModel *model = self.superModel.nodes[indexPath.row];
    
    QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[model.type isEqualToString:@"Master"]?[UIImage imageNamed:@"arrow"]:nil forState:UIControlStateNormal];
    [btn setTitleColor:kCOLOR_HEX(@"666666") forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [btn setImagePosition:QMUIButtonImagePositionRight];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [btn setFrame:CGRectMake(0, 0, 100, 40)];
    [cell setAccessoryView:btn];
    
    [cell.textLabel setText:model.type];
    [btn setTitle:model.sn forState:UIControlStateNormal];
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RHMQEndPointModel *model = self.superModel.nodes[indexPath.row];
    if([model.type isEqualToString:@"Master"]){
        RHMQEndPointNodesController *vc = [[RHMQEndPointNodesController alloc] initWithModel:model];
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}


@end
